package com.iseastar.guojiang.cabinet.adapter;

import java.util.ArrayList;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.iseastar.guojiang.cabinet.CabinetParcelsListActivity;
import com.iseastar.guojiang.model.OrderBean;
import com.shouhuobao.station.R;

import droid.frame.activity.base.BaseAdapterWithImage;
import droid.frame.utils.http.ImageCallback;

/**
 * 智能柜、包裹列表
 *
 * @author coffee<br>
 *         2016-12-21下午3:57:45
 */
public class CabinetParcelsListAdapter extends BaseAdapterWithImage<OrderBean> {

	private CabinetParcelsListActivity activity;

	public CabinetParcelsListAdapter(ArrayList<OrderBean> items, CabinetParcelsListActivity context, ViewGroup viewGroup) {
		super(items, context, viewGroup);
		activity = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.cabinet_grid_item, parent, false);
			holder.noBagCode = (TextView) convertView.findViewById(R.id.cabinet_bagcode_title);
			holder.bagCode = (TextView) convertView.findViewById(R.id.cabinet_bagcode);
			holder.openCode = (TextView) convertView.findViewById(R.id.cabinet_boxcode);
			holder.action = (Button) convertView.findViewById(R.id.cabinet_check);
			holder.img = (ImageView) convertView.findViewById(R.id.cabinet_detail_img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		OrderBean item = items.get(position);
		showImage(item.getParcelImgUrl(), holder.img, new ImageCallback() {
			@Override
			public void onFailed() {
				holder.img.setImageResource(R.drawable.parcel_goods_default);
			}
		});
		if (TextUtils.isEmpty(item.getBagCode())||"null".equals(item.getBagCode())){
			holder.noBagCode.setText("无袋验证码:");
			holder.bagCode.setText(item.getPassword2());// 无袋验证码
			holder.action.setText("绑定投递编号");
		}else {
			holder.noBagCode.setText("投递编号:");
			holder.bagCode.setText(item.getBagCode());// 设备编码
			holder.action.setText("验收包裹");
		}
		holder.openCode.setText(item.getCheckCode());
		holder.action.setTag(item);
		holder.action.setOnClickListener(activity);
		return convertView;
	}

	private static class ViewHolder {
		TextView bagCode, openCode,noBagCode;
		Button action;
		ImageView img;
	}
}
